Partial Class Admin_Add_Providers
  Inherits System.Web.UI.Page

    Private Sub SecurePage()
        Dim sec As New Secure

        sec.ParentPage = Me
        sec.AddDataGridColumn(Me.DataGrid, DataGrid.Columns.Count - 2, Secure.ControlTypes.DeleteType)
        sec.AddDataGridColumn(Me.DataGrid, DataGrid.Columns.Count - 3, Secure.ControlTypes.EditType)
        sec.SetPageAccess(Session("UserName"))
        sec = Nothing
    End Sub

  Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Country_cmb.Items.Count < 1 Then

            Dim zClass As New LookUp.General_Lookup
            Country_cmb.Items.Clear()
            zClass.Loader(Country_cmb, "COUNTRY", "country_desc", "country_cd")
            Country_cmb.Items.Insert(0, "")
            brokers_cmb.Items.Clear()
            zClass.Loader(brokers_cmb, "BROKER", "broker_desc", "broker_cd")
            brokers_cmb.Items.Insert(0, "")

            zClass.LookUpCode = "PROVIDER_CONTACT"
            DataGrid.CurrentPageIndex = 0
            If Session("provider_mode") = "View" Then
                zClass.DataGridFill(DataGrid, Results_lbl, Session("provider_cd"))
                LoadDetails()
                DisableMost()
            ElseIf Session("provider_mode") = "Edit" Then
                zClass.DataGridFill(DataGrid, Results_lbl, Session("provider_cd"))
                LoadDetails()
                brokers_cmb.Enabled = False
            End If

            If Not IsNothing(Session("broker_cd")) Then
                brokers_cmb.SelectedValue = Session("broker_cd")
                brokers_cmb.Enabled = False
            End If
        End If
        SecurePage()
  End Sub

  Protected Sub Country_cmb_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Country_cmb.SelectedIndexChanged
    If Country_cmb.SelectedItem.Text = "" Then Exit Sub

    Dim zClass As New LookUp.General_Lookup
    City_cmb.Items.Clear()
    zClass.Loader(City_cmb, "CITY", "city_desc", "city_cd", Country_cmb.SelectedValue)
    City_cmb.Items.Insert(0, "")

  End Sub

  Private Sub DisableMost()
    DataGrid.Columns(5).Visible = False : DataGrid.Columns(6).Visible = False
    Save_btn.Enabled = False : Website_txt.Enabled = False
    Name_txt.Enabled = False : Add1_txt.Enabled = False : Add2_txt.Enabled = False
    City_cmb.Enabled = False : Country_cmb.Enabled = False : Zip_txt.Enabled = False
    T1_txt.Enabled = False : M1_txt.Enabled = False : F1_txt.Enabled = False
    Add_btn.Enabled = False : brokers_cmb.Enabled = False
  End Sub

  Private Sub LoadDetails()
    Dim sConn As New Data.SqlClient.SqlConnection
    Dim sCom As New Data.SqlClient.SqlCommand
    Dim sDApt As New Data.SqlClient.SqlDataAdapter(sCom)
    Dim sDT As New Data.DataTable
    Dim zClass As New LookUp.General_Lookup

    Try
      sConn.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("CnnStr")
      sCom.CommandType = Data.CommandType.Text
      sCom.Connection = sConn
      sCom.CommandText = "SELECT * FROM provider_mstr WHERE provider_cd='" & Session("provider_cd") & "'"
      sConn.Open()
      sDApt.Fill(sDT)

      With sDT.Rows(0)

        brokers_cmb.SelectedItem.Selected = False
        If Trim(.Item(1).ToString) <> "" Then brokers_cmb.Items.FindByValue(.Item(1).ToString).Selected = True

        Name_txt.Text = .Item(2).ToString
        Add1_txt.Text = .Item(3).ToString : Add2_txt.Text = .Item(4).ToString

        Country_cmb.SelectedItem.Selected = False
        If Trim(.Item(6).ToString) <> "" Then Country_cmb.Items.FindByValue(.Item(6).ToString).Selected = True

        City_cmb.Items.Clear()
        zClass.Loader(City_cmb, "CITY", "city_desc", "city_cd", Country_cmb.SelectedValue)
        City_cmb.Items.Insert(0, "")
        If Trim(.Item(5).ToString) <> "" Then City_cmb.Items.FindByValue(.Item(5).ToString).Selected = True

        Zip_txt.Text = Trim(.Item(7).ToString) : T1_txt.Text = Trim(.Item(8).ToString)
        M1_txt.Text = Trim(.Item(10).ToString) : F1_txt.Text = Trim(.Item(9).ToString)
        Website_txt.Text = .Item(11).ToString

      End With

    Catch ex As Exception

    Finally
      sConn.Close()
    End Try

  End Sub

  Protected Sub Cancel_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Cancel_btn.Click
    Session.Remove("provider_cd") : Session.Remove("provider_mode")
    Response.Redirect("~/Admin/Providers.aspx")
  End Sub

  Protected Sub Save_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Save_btn.Click
    Dim zClass As New LookUp.General_Lookup
    Dim dm As New DAOMain.CommonDAO
    Dim strTemp As String = ""

    If Session("provider_mode") = "Edit" Then
            zClass.ExecQuery("UPDATE provider_mstr SET provider_desc=" & UCase(dm.Q(Trim(Name_txt.Text))) & _
                ", provider_address=" & UCase(dm.Q(Trim(Add1_txt.Text))) & _
                ", provider_address2=" & UCase(dm.Q(Trim(Add2_txt.Text))) & _
                ", city_cd=" & UCase(dm.Q(Trim(City_cmb.SelectedValue))) & _
                ", country_cd=" & UCase(dm.Q(Trim(Country_cmb.SelectedValue))) & _
                ", zip_cd=" & UCase(dm.Q(Trim(Zip_txt.Text))) & _
                ", tel_no=" & UCase(dm.Q(Trim(T1_txt.Text))) & _
                ", fax_no=" & UCase(dm.Q(Trim(F1_txt.Text))) & _
                ", mobile_no=" & UCase(dm.Q(Trim(M1_txt.Text))) & _
                ", provider_web_site=" & UCase(dm.Q(Trim(Website_txt.Text))) & _
                ", provider_last_upd_by='" & UCase(Session("UserName")) & "', provider_last_upd_dt='" & _
                Now & "'" & _
                " WHERE provider_cd='" & Trim(Session("provider_cd").ToString) & "'")
            Session("provider_cd") = strTemp

    ElseIf Session("provider_mode") = "Add" Then
      'must have city, country, start_date, and name

      zClass.LookUpCode = "PROVIDER"
      strTemp = zClass.MaxCode()
            zClass.ExecQuery("INSERT INTO provider_mstr(provider_cd, broker_cd, provider_desc, provider_address, " & _
                "provider_address2, city_cd, country_cd, zip_cd, tel_no, fax_no, mobile_no, " & _
                "provider_web_site, provider_crea_by, provider_crea_dt) " & _
                " VALUES('" & strTemp & "'" & _
                ",'" & Trim(brokers_cmb.SelectedValue) & "'" & _
                "," & UCase(dm.Q(Trim(Name_txt.Text))) & _
                "," & UCase(dm.Q(Trim(Add1_txt.Text))) & _
                "," & UCase(dm.Q(Trim(Add2_txt.Text))) & _
                "," & UCase(dm.Q(Trim(City_cmb.SelectedValue))) & _
                "," & UCase(dm.Q(Trim(Country_cmb.SelectedValue))) & _
                "," & UCase(dm.Q(Trim(Zip_txt.Text))) & _
                "," & UCase(dm.Q(Trim(T1_txt.Text))) & _
                "," & UCase(dm.Q(Trim(F1_txt.Text))) & _
                "," & UCase(dm.Q(Trim(M1_txt.Text))) & _
                "," & UCase(dm.Q(Trim(Website_txt.Text))) & _
                ",'" & UCase(Session("UserName")) & "'" & _
                ",'" & Now & "')")
            Session("provider_cd") = strTemp
    End If

    Session("provider_mode") = "Edit"
    zClass.LookUpCode = "PROVIDER_CONTACT"
    DataGrid.CurrentPageIndex = 0
    zClass.DataGridFill(DataGrid, Results_lbl, Session("provider_cd"))
    LoadDetails()
    'Response.Redirect("~/Admin/Brokers.aspx")
  End Sub

  Protected Sub DataGrid_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DataGrid.ItemCommand
    If e.CommandName = "Edit" Or e.CommandName = "View" Then
      Session("contact_cd") = e.Item.Cells(0).Text
      Session("contact_mode") = e.CommandName
      Response.Redirect("~/Admin/Add_Contact_Provider.aspx")

    ElseIf e.CommandName = "Delete" Then
      Dim zClass As New LookUp.General_Lookup

      'If zClass.OKtoDELETE(e.Item.Cells(0).Text) = True Then
      zClass.ExecQuery("DELETE FROM provider_contact_dtl WHERE provider_contact_cd='" & _
        e.Item.Cells(0).Text & "' AND provider_cd='" & Session("provider_cd") & "'")

      zClass.LookUpCode = "PROVIDER_CONTACT"
      DataGrid.CurrentPageIndex = 0
      zClass.DataGridFill(DataGrid, Results_lbl, Session("provider_cd"))
    End If
  End Sub

  Protected Sub Add_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Add_btn.Click
    Session("contact_mode") = "Add"
    Response.Redirect("~/Admin/Add_Contact_Provider.aspx")

  End Sub

  Protected Sub DataGrid_PageIndexChanged(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles DataGrid.PageIndexChanged
    DataGrid.CurrentPageIndex = e.NewPageIndex
    Dim zClass As New LookUp.General_Lookup
    zClass.LookUpCode = "PROVIDER_CONTACT"
    zClass.DataGridFill(DataGrid, Results_lbl, Session("provider_cd"))

  End Sub
End Class